package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.core.FzException;
import com.fz.entity.Category;
import com.fz.entity.Collect;
import com.fz.entity.Goods;
import com.fz.entity.User;
import com.fz.mapper.CollectMapper;
import com.fz.service.CategoryService;
import com.fz.service.CollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fz.service.GoodsService;
import com.fz.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 存储收藏的商品信息 服务实现类
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {
    @Resource
    private UserService tuserService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private CategoryService categoryService;
    @Override
    public Boolean add(Long userId, Long goodsId) {
        User user = tuserService.getById(userId);
        Goods goods = goodsService.getById(goodsId);
        if (user == null || goods == null) {
            throw new FzException("用户或者商品不存在");
        }
        LocalDateTime now = LocalDateTime.now();
        Collect collect = new Collect(goods.getName(), goods.getImgs(),goodsId ,  userId,LocalDateTime.now(),goods.getCategoryId());
        return this.save(collect);
    }

    @Override
    public Boolean search(Long userId, Long goodsId) {
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("goods_id", goodsId);
        return this.count(wrapper)==0;
    }

    @Override
    public IPage<Collect> list(Long userId, Integer pageNo, Integer pageSize, Long selectedCategory) {
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        System.out.println("进入收藏list"+selectedCategory+"2:");
        List<Category> leafNodes = categoryService.findAllLeafNodes(selectedCategory);
        List<Long> categoryleafNodes = new ArrayList<>();
        for (Category leafNode : leafNodes) {
            categoryleafNodes.add(leafNode.getId());
        }
        wrapper.in("category_id", categoryleafNodes);
        IPage<Collect> page = this.page(new Page<>(pageNo, pageSize), wrapper);
        page.getRecords().forEach(collect -> {
            String imgs = collect.getGoodsImg();
            int semicolonIndex = imgs.indexOf(';');
            String firstImg = semicolonIndex != -1 ? imgs.substring(0, semicolonIndex) : imgs;
            collect.setGoodsImg(firstImg);
            Goods goods = goodsService.getById(collect.getGoodsId());
            if (goods != null) {
                collect.setPrice(goods.getPrice());
            }
        });
        return page;
    }

    @Override
    public Boolean delete(Long userId, Long goodsId) {
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("goods_id", goodsId);
        return this.remove(wrapper);
    }
}
